'''
@文件    :views.py
@说明    :
@时间    :2020/04/02 23:55:24
@作者    :didiplus
@版本    :1.0
'''

from flask import Blueprint,make_response,jsonify,json,request
from .__init__ import Send,request_query,BASE_URL
song = Blueprint('song',__name__,url_prefix='/api/song')

#获取单曲
@song.route('/url',methods=['POST', 'GET'])
def url():
    query = request_query(request, ["id", "ids"], ["br", {"br": "999000"}])
    query["ids"] = "[" + query["ids"] + "]"
    data = Send(query).POST('weapi/song/enhance/player/url')
    response = make_response(jsonify(json.loads(data.text)),403)
    return response

#获取单曲歌词
@song.route('/lyric',methods=['POST','GET'])
def lyric():
    query = request_query(request,"id")
    data = Send({"url":BASE_URL + "api/song/lyric?lv=-1&kv=-1&tv=-1","params":query},"linuxapi").POST("")
    return make_response(jsonify(json.loads(data.text)),403)

#检查歌曲是否可用
@song.route('/check',methods=['POST','GET'])
def check():
    init_data = {}
    query = request_query(request,["id", "ids"], "br")
    query["ids"] = "[" + query["ids"] + "]"
    temp = query.copy()
    if "br" not in query or query["br"] == None:
        temp["br"] = "999000"
    data = json.loads(Send(temp).POST("weapi/song/enhance/player/url").content)
    br = data['data'][0]['br']
    if not query['br']:
        init_data['maxbr'] = br
    else:
        if br < int(query['br']):
            init_data['br'] = False
        else:
            init_data['br'] = True

    return make_response(jsonify(str(init_data)),403)

#获取音乐详情
@song.route('/detail',methods=['POST','GET'])
def detail():
    query = request_query(request,["id","ids"])
    query['c'] = '[' + \
        ",".join(['{"id":' + i + '}' for i in query["ids"].split(",")]) + ']'
    query['ids'] = "[" + query['ids'] + "]"
    data = Send(query).POST("weapi/v3/song/detail")
    return make_response(jsonify(json.loads(data.text)),403)


#获取每日推荐
@song.route('/recommend',methods=['POST','GET'])
def recommend():
    data = Send({"total": "true"}).POST("weapi/v1/discovery/recommend/songs")
    return make_response(jsonify(json.loads(data.text)),403)


#推荐新歌
@song.route('/newsong',methods=['POST','GET'])
def newsong():
    data = Send({"type":"recommend"}).POST('weapi/personalized/newsong')
    return make_response(jsonify(json.loads(data.text)),403)

#获取最近5个听了这首歌的用户
@song.route('/simiuser',methods=['POST','GET'])
def simiuser():
    query = request_query(request,['id','songid'])
    data = Send(query).POST('weapi/discovery/simiUser')
    return make_response(jsonify(json.loads(data.text)),403)


#新碟上级
@song.route('/new',methods=['POST','GET'])
def new():
    # 全部：ALL 国内：ZH 欧美：EA 韩国：KR 日本：JP
    query = request_query(request,["area",{"area":"ALL"}], ["limit", {"limit": 50}],["offset", {"offset": 0}])
    query['total'] = True
    data = Send(query).POST("weapi/album/new")
    return make_response(jsonify(json.loads(data.text)),403)

#给喜欢音乐的音乐点小红心
@song.route('like',methods=['POST','GET'])
def like():
    query = request_query(request,
                          ["id", "trackId"],
                          "like",
                          ["alg", {"alg": "itembased"}],
                          ["time", {"time": 25}])
    url = "weapi/radio/like?alg={}&trackId={}&time={}".format(
        query["alg"], query["trackId"], query["time"])
    data = Send({"trackId": query["trackId"], "like": query["like"]}).POST(url)
    return make_response(jsonify(json.loads(data.text)),403)

#智能播放/心动模式
@song.route('/intelligence',methods=['POST','GET'])
def intelligence():
    query = request_query(request,
                          ["id", "songId"],
                          ["pid", "playlistId"],
                          ["sid", {"startMusicId": ""}],
                          ["count", {"count": 1}])
    if query["startMusicId"] == "":
        query["startMusicId"] = query["songId"]
    query["type"] = "fromPlayOne"
    data = Send(query).POST("weapi/playmode/intelligence/list")
    return make_response(jsonify(json.loads(data.text)),403)
